﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MarchEstate.Manager.BusinesEntity;
using System.Data;


public class CarportController : AbstractJqueryController
{
    public void GetCarAreaData()
    {
        AbstractCarArea carArea = NewObject<AbstractCarArea>();
        DataTable carAreaList = carArea.GetCarAreaList();
        TxtJson = base.DataTableToJson(carAreaList);
    }

    public void GetCarportData()
    {
        AbstractCarport carport = NewObject<AbstractCarport>();
        TxtJson = base.ListToJson(carport.GetCarportList(ParamsData["_statCode"]));
    }

    /// <summary>
    /// 保存车位区域
    /// </summary>
    public void SaveCarArea()
    {
        try
        {
            string code = FormData["Code"];
            string areaName = FormData["AreaName"];
            string type = FormData["type"];
            AbstractCarArea carArea = NewObject<AbstractCarArea>();
            carArea.Code = code;
            carArea.AreaName = areaName;
            carArea.Save(type);
            TxtJson = ReturnSuccess("维护成功");
        }
        catch (Exception ex)
        {
            TxtJson = ReturnError(ex.Message);
        }
    }

    #region 删除车位区域
    /// <summary>
    /// 删除车位区域
    /// </summary>
    public void DeleteCarArea()
    {
        try
        {
            string code = Convert.ToString(ParamsData["Code"]);
            AbstractCarArea carArea = NewObject<AbstractCarArea>();
            if (carArea.Delete(code))
            {
                TxtJson = ReturnSuccess("删除车位区域成功！");
            }
            else
            {
                TxtJson = ReturnSuccess("删除车位区域失败！");
            }
        }
        catch (Exception ex)
        {
            TxtJson = ReturnError(ex.Message);
        }

    }
    #endregion  

    public void GetCarAreaList()
    {
        AbstractCarArea carArea = NewObject<AbstractCarArea>();
        DataTable carAreaList = carArea.GetCarAreaList();
        TxtJson = Newtonsoft.Json.JavaScriptConvert.SerializeObject(carAreaList);
    }


    #region 删除车位区域
    /// <summary>
    /// 删除车位区域
    /// </summary>
    public void DeleteCarport()
    {
        try
        {
            string _carPortId = Convert.ToString(ParamsData["CarPortId"]);
            AbstractCarport carport = NewObject<AbstractCarport>();
            if (carport.Delete(_carPortId))
            {
                TxtJson = ReturnSuccess("删除车位成功！");
            }
            else
            {
                TxtJson = ReturnSuccess("删除车位失败！");
            }
        }
        catch (Exception ex)
        {
            TxtJson = ReturnError(ex.Message);
        }

    }
    #endregion  

    public void SaveCarport()
    {
        try
        {
            AbstractCarport carport = GetModelToForm<AbstractCarport>();
            carport.Save();
            TxtJson = ReturnSuccess("");
        }
        catch (Exception ex)
        {
            TxtJson = ReturnError(ex.Message);
        }
    }

    public void BatchSaveCarport()
    {
        try
        {
            string carportcode_begin = ParamsData["carportcode_begin"];
            string carportcode_end = ParamsData["carportcode_end"];
            string carportareacode = ParamsData["carportareacode"];
            string carportarea = ParamsData["carportarea"];
            string carportplace = ParamsData["carportplace"];

            int length = carportcode_end.Length;
            int begin = Convert.ToInt32(carportcode_begin);
            int end = Convert.ToInt32(carportcode_end);

            AbstractCarport carport = NewObject<AbstractCarport>();
            for (int i = begin; i <= end; i++)
            {
                AbstractCarport _carport = (AbstractCarport)carport.Clone();
                _carport.Code = getcarportcode(i, length);
                _carport.CarAreaCode = carportareacode;
                _carport.Area = Convert.ToDecimal(carportarea);
                _carport.Place = carportplace;
                _carport.Save();
            }

            TxtJson = ReturnSuccess("");
        }
        catch (Exception ex)
        {
            TxtJson = ReturnError(ex.Message);
        }
    }

    private string getcarportcode(int num, int length)
    {
        int count = length - num.ToString().Length;
        string str = "";
        for (int i = 0; i < count; i++) { str += "0"; }
        return str + num.ToString();
    }
}

